<?php

namespace EM\NewsBundle\Controller;
use EM\NewsBundle\Entity\Categories;
use EM\NewsBundle\Form\CategoryType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class CategoryController extends Controller
{
    
    public function createAction(Request $request)
    {
	
                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Categories');
                $categories = $repository->findAll();
		$category = new Categories();
		$form = $this->createForm(new CategoryType(), $category);
		$request = $this->getRequest();
		if($request->getMethod() == 'POST')
		{
			
			$form->bind($request);

			if($form->isValid())
			{
				$em = $this->getDoctrine()->getManager();
				
                                if($category->getCategoryParent()==null){
                                   $category->setLevel(0); 
                                }
                                else
                                {
                                    $newLevel=($category->getCategoryParent()->getLevel())+1;
                                    if($newLevel==4)
                                    {
                                       return $this->render('EMNewsBundle:Category:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'categories' => $categories,
                                    'status_form'=>3
                                    )); 
                                    }
                                    else
                                    {
                                        $category->setLevel($newLevel);
                                    }
                                }
                                $em->persist($category);
				$em->flush();
                                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Categories');
                                $categories = $repository->findAll();
                                $category=new Categories();
                                $form = $this->createForm(new CategoryType(), $category);
				return $this->render('EMNewsBundle:Category:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'categories' => $categories,
                                    'status_form'=>1
                                    )); 
			}
			else
			{
			return $this->render('EMNewsBundle:Category:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'categories' => $categories,
                                    'status_form'=>2
                                    )); 
			}
		
		}
                
    return $this->render('EMNewsBundle:Category:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'categories' => $categories,
                                    'status_form'=>0
                                    )); 
			            

    }

    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $repository=$em->getRepository('EMNewsBundle:Categories');
        $categories = $repository->findAll();	
        $category=$repository->find($id);             
        $form = $this->createForm(new CategoryType(), $category);
        $request = $this->getRequest();

            if($request->getMethod() == 'POST')
            {
                if($category->getCategoryParent()!==null){
                    $previousParent=$category->getCategoryParent()->getId(); 
                }
                else
                {
                     $previousParent=null;
                }
                $form->bind($request);		
                    if($form->isValid())
                    {
                            
                             if($category->getCategoryParent()!==null){
                                  $combo=$category->getCategoryParent()->getId();
                                }
                                else
                                {
                                    $combo=null;
                                }
                            if($previousParent==$combo)
                            {
                                //if combo doesn't change the value, save object.
                                $em = $this->getDoctrine()->getManager();
                                $em->persist($category);
                                $em->flush();
                                return $this->render('EMNewsBundle:Category:_form.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>5
                                    )); 
                            }
                            else
                            {
                                if($combo==$category->getId())
                                {
                                    //this values cant be equals.
                                    return $this->render('EMNewsBundle:Category:_form.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>2
                                    ));
                                }
                                else
                                {
                                  $categoryMatch=$repository->findBy(array('categoryParent' => $category->getId()));
                                  if(count($categoryMatch)!=0)
                                  {
                                    
                                    return $this->render('EMNewsBundle:Category:_form.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>3
                                    ));  
                                  }

                                }
                                $newLevel=($category->getCategoryParent()!=null?$category->getCategoryParent()->getLevel()+1:0);
                                if($newLevel>3)
                                {
                                   return $this->render('EMNewsBundle:Category:_form.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>4
                                    )); 
                                }
                                $category->setLevel($newLevel);
                                $em->persist($category);
                                $em->flush();
                                return $this->render('EMNewsBundle:Category:_form.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>5
                                    )); 
                            }
                                
                           // $em->persist($category);
                           // $em->flush();
                           // return $this->redirect($this->generateUrl('em_category_list'));
                            
                    }
                   
            }
            return $this->render('EMNewsBundle:Category:_form.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>2
                                    )); 
        
    }
    public function deleteAction($id)
    {
                
		$em = $this->getDoctrine()->getManager();
                $repository=$em->getRepository('EMNewsBundle:Categories');
                $category=$repository->find($id);  
		$em->remove($category);
		$em->flush();
                $category = new Categories();
		$form = $this->createForm(new CategoryType(), $category);
                $categories = $repository->findAll();
                return $this->render('EMNewsBundle:Category:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'categories' => $categories,
                                    'status_form'=>0
                                    )); 
    }

     public function showAction(){

            $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Categories');
            $categories = $repository->findAll();	
            $category = new Categories();
            $form = $this->createForm(new CategoryType(), $category);
            return $this->render('EMNewsBundle:Category:list.html.twig',array('categories' => $categories,  'form' => $form->createView(),'status_form'=>0));

    } 
    public function findAction($id){
         
        $em = $this->getDoctrine()->getManager();
        $repository=$em->getRepository('EMNewsBundle:Categories');
        $category=$repository->find($id);
        $categories = $repository->findAll();	
        $form = $this->createForm(new CategoryType(), $category);

        return $this->render('EMNewsBundle:Category:_form.html.twig', array(
				'form' => $form->createView(),
				'id' => $category->getId(),
                                'categories' => $categories,
				'status_form'=>0
                            ));
        
    }
	
}
